package o;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.content.pm.ProviderInfo;
import android.os.AsyncTask;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import java.io.File;
import java.util.List;
import o.cof.a;

public final class cpp extends AsyncTask<Void, Void, Void> {
    private long ʼ;
    private int ˊ;
    private boolean ˋ;
    private final int ˎ;
    private final String ˏ;
    private Context ॱ;

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return ˋ((Void[]) objArr);
    }

    private cpp(Context context, String str, int i, long j, int i2, boolean z) {
        this.ॱ = context.getApplicationContext();
        this.ˏ = str;
        this.ˎ = i;
        this.ʼ = j;
        this.ˊ = i2;
        this.ˋ = z;
    }

    public static void ˏ(Context context, String str, int i, long j, int i2, boolean z) {
        new cpp(context, str, i, j, i2, z).execute(new Void[0]);
    }

    protected Void ˋ(Void... voidArr) {
        cpc ˎ = cpc.ˎ(this.ॱ);
        ˎ.ॱ();
        if (!ॱ()) {
            if (1 != this.ˎ) {
                ˎ();
            } else {
                ˋ();
            }
        }
        ˎ.ˏ();
        return null;
    }

    private void ˎ() {
        ManagerTask ˏ = cpa.ˊ().ˏ(this.ˏ, cod.INSTALL, cod.INSTALL_EXISTING_PKG);
        if (ˏ != null && ˏ.taskId == this.ʼ) {
            if (this.ˋ) {
                ˏ.ˏ = true;
                File ॱ = new cpo().ॱ(this.ॱ);
                File file = new File(((a) ˏ.apkInfos.get(0)).ˎ);
                if (!file.exists() || ॱ == null) {
                    cnu.ˏ.ˊ("DealTheTaskWhenInstalled", "file do not exist Or can not find backupPath,can not change file path to retry install");
                } else {
                    File file2 = new File(ॱ, file.getName());
                    String path = file2.getPath();
                    if (new cpo().ˊ(this.ॱ, file, file2)) {
                        ˏ(ˏ, file2, path);
                        return;
                    }
                    cnu.ˏ.ˊ("DealTheTaskWhenInstalled", "rename failed, can not change file path to retry install");
                }
            }
            ˎ(ˏ);
            if (cod.INSTALL == ˏ.processType) {
                if (cop.ॱ().ˎ(this.ˎ, ˏ)) {
                    cpq.ˋ(ˏ);
                } else {
                    cpa.ˊ().ˋ(this.ॱ, ˏ);
                }
            }
            if (cph.ॱ(this.ॱ).ˋ(this.ˏ, 4, cnz.NOT_HANDLER, this.ˎ, this.ˊ, this.ʼ, cod.INSTALL, cod.INSTALL_EXISTING_PKG) == null) {
                cnu.ˏ.ˊ("DealTheTaskWhenInstalled", "should never get null.something is wrong.");
            }
        }
    }

    private void ˎ(ManagerTask managerTask) {
        if (-13 == this.ˎ) {
            ˋ(managerTask);
        } else if (-112 == this.ˎ) {
            ˏ(managerTask);
        }
    }

    private void ˏ(ManagerTask managerTask, File file, String str) {
        cnu.ˏ.ˋ("DealTheTaskWhenInstalled", " try install again,rename to newPath:" + str);
        File parentFile = file.getParentFile();
        if (parentFile != null) {
            parentFile.setExecutable(true, false);
        }
        file.setReadable(true, false);
        ((a) managerTask.apkInfos.get(0)).ˎ = str;
        cpc.ˎ(this.ॱ).ˏ(managerTask);
        cph.ˋ(this.ॱ, managerTask, "install|pkg:" + managerTask.packageName + "|path:" + str + "|flag:" + managerTask.flag + "|changePath");
    }

    private void ˋ() {
        ManagerTask ˋ = cph.ॱ(this.ॱ).ˋ(this.ˏ, 5, cnz.INSTALL_FINISH, 0, this.ˊ, this.ʼ, cod.INSTALL, cod.INSTALL_EXISTING_PKG);
        if (ˋ != null) {
            cnu.ˏ.ˋ("DealTheTaskWhenInstalled", "DealTheTaskWhenInstalled pkg :" + this.ˏ);
            if (cop.ॱ().ˋ(ˋ)) {
                cpq.ˋ(ˋ);
            }
        }
    }

    private boolean ॱ() {
        if (this.ˎ != -117) {
            return false;
        }
        ManagerTask ˏ = cpa.ˊ().ˏ(this.ˏ, cod.INSTALL, cod.INSTALL_EXISTING_PKG);
        if (ˏ == null || ˏ.taskId != this.ʼ || ˏ.ˎ) {
            return false;
        }
        Intent intent = new Intent("AppProfile.InstallFailed");
        intent.putExtra("AppProfile.DownloadResult.Param.PackageName", ˏ.packageName);
        intent.putExtra("AppProfile.DownloadResult.Param.VersionCode", ˏ.versionCode);
        ll.ˏ(this.ॱ).ˋ(intent);
        ˏ.ˎ = true;
        cph.ˋ(this.ॱ, ˏ, "install|pkg:" + ˏ.packageName + "|path:" + ((a) ˏ.apkInfos.get(0)).ˏ + "|flag:" + ˏ.flag + "|disProf");
        return true;
    }

    private void ˋ(ManagerTask managerTask) {
        PackageManager packageManager = this.ॱ.getPackageManager();
        for (a aVar : managerTask.apkInfos) {
            PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(aVar.ˎ, 8);
            if (!(packageArchiveInfo == null || packageArchiveInfo.providers == null || packageArchiveInfo.providers.length <= 0)) {
                List<PackageInfo> installedPackages = packageManager.getInstalledPackages(8);
                if (installedPackages != null && installedPackages.size() > 0) {
                    for (PackageInfo packageInfo : installedPackages) {
                        if (packageInfo.providers != null) {
                            for (ProviderInfo providerInfo : packageArchiveInfo.providers) {
                                for (ProviderInfo providerInfo2 : packageInfo.providers) {
                                    if (providerInfo.authority.equals(providerInfo2.authority)) {
                                        managerTask.ˊॱ = packageInfo.packageName;
                                        managerTask.ͺ = packageInfo.applicationInfo.loadLabel(packageManager).toString();
                                        managerTask.ˋॱ = providerInfo.authority;
                                        return;
                                    }
                                }
                            }
                            continue;
                        }
                    }
                    continue;
                }
            }
        }
    }

    private void ˏ(ManagerTask managerTask) {
        PackageManager packageManager = this.ॱ.getPackageManager();
        for (a aVar : managerTask.apkInfos) {
            PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(aVar.ˎ, 4096);
            if (!(packageArchiveInfo == null || packageArchiveInfo.permissions == null || packageArchiveInfo.permissions.length <= 0)) {
                List<PackageInfo> installedPackages = packageManager.getInstalledPackages(4096);
                if (installedPackages != null && installedPackages.size() > 0) {
                    for (PackageInfo packageInfo : installedPackages) {
                        if (packageInfo.permissions != null) {
                            for (PermissionInfo permissionInfo : packageArchiveInfo.permissions) {
                                for (PermissionInfo permissionInfo2 : packageInfo.permissions) {
                                    if (permissionInfo.name.equals(permissionInfo2.name)) {
                                        managerTask.ˊॱ = packageInfo.packageName;
                                        managerTask.ͺ = packageInfo.applicationInfo.loadLabel(packageManager).toString();
                                        managerTask.ˋॱ = permissionInfo.name;
                                        return;
                                    }
                                }
                            }
                            continue;
                        }
                    }
                    continue;
                }
            }
        }
    }
}
